import {
  Controller,
  Request,
  Get,
  Post,
  UseGuards,
  Body,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiTags } from '@nestjs/swagger';
import { AuthService } from '../services/auth.service';
import { User } from '@/modules/users/entities/user.entity';
import { UserRegisterDto } from '../dto/user-register.dto';
import { CaptchaGuard } from '@/guards';

@ApiTags('auth')
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  // @UseGuards(CaptchaGuard, AuthGuard('local'))
  @UseGuards(AuthGuard('local'))
  @Post('login')
  async login(@Request() req) {
    return await this.authService.login(req.user as User);
  }

  @UseGuards(AuthGuard())
  @Post('logout')
  async logout(@Request() req) {
    return await this.authService.logout(req.user as User);
  }

  @UseGuards(CaptchaGuard)
  @Post('register')
  async register(@Body() userRegisterDto: UserRegisterDto) {
    return await this.authService.register(userRegisterDto);
  }

  @UseGuards(AuthGuard())
  @Get('me')
  async info(@Request() req) {
    return await this.authService.getUser(req.user.id);
  }
}
